package com.kkb.service;

import com.kkb.mapper.UserMapper;
import com.kkb.pojo.User;
import com.kkb.pojo.UserExample;
import com.kkb.vo.UserQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lxy
 * @create 2022-03-18 14:54
 */

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;


    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<User> getUser(UserQueryVo vo){
        UserExample userExample = new UserExample();
        //查询用户需要用到的条件容器
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andU_loginNameEqualTo(vo.getUsername());
        criteria.andU_passWordEqualTo(vo.getPassword());
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList != null){
            return userList;
        }
        return null;
    }

    //处理密码修改
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updatePass(String newpass,User user){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        //根据账号密码确认一个用户，单靠密码或者账号拿到的用户不唯一！
        criteria.andU_loginNameEqualTo(user.getU_loginName());
        criteria.andU_passWordEqualTo(user.getU_passWord());
        User newUser = new User();
        newUser.setU_passWord(newpass);
        return userMapper.updateByExampleSelective(newUser,userExample);
    }
}
